<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { toNumber } from 'lodash-es'
import useClipboard from 'vue-clipboard3'
import { GetData, patchLandingpageType } from '@/api/modules/videoManagement.ts'

const router = useRouter()
const imgDomain = 'https://img1.eprhan.cc'

// 查询的表单
const inquireFrom = ref({
  LandingName: '',
})
const loading = ref(true)

const dataList: any = ref([])
let PageSize = 10
let PageNum = toNumber(localStorage.getItem('currentPage')) || 1
const total = ref(0) // 总数据数

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}
// 新增
function addData() {
  router.push({
    name: 'videoManagementDetail',
  })
}
// 编辑
function update(id: number) {
  localStorage.setItem('currentPage', `${PageNum}`)
  router.push({
    name: 'videoManagementDetail',
    query: {
      id,
    },
  })
}
// 复制地址
async function copy(id: number) {
  const clipboard = useClipboard()
  try {
    // 使用 navigator.clipboard.writeText 来复制文本
    // await navigator.clipboard.writeText(`pages/adPage/adPage?landing_id=${id}&pushCoupons=0`)
    await clipboard.toClipboard(`pages/adPage/adPage?landing_id=${id}&pushCoupons=0`)
    ElMessage.success('复制成功')
  }
  catch (error) {
    const textarea = document.createElement('textarea')
    textarea.value = `pages/adPage/adPage?landing_id=${id}&pushCoupons=0`
    document.body.appendChild(textarea)
    textarea.select()
    // 使用现代浏览器支持的复制命令
    const successful = document.execCommand('copy')
    if (!successful) {
      ElMessage.error('复制失败')
    }
    else {
      ElMessage.success('复制成功')
    }
    document.body.removeChild(textarea)
  }
}
// 获取数据
function GetDataList(PageIndex: number = 1, PageSize: number = 10) {
  loading.value = true
  const params = {
    PageIndex,
    PageSize,
    ...(inquireFrom.value.LandingName && { LandingName: inquireFrom.value.LandingName }),
  }
  GetData(params).then((res: any) => {
    if (res.succeeded) {
      dataList.value = res.data.items
      total.value = res.data.total
      loading.value = false
    }
    else {
      ElMessage.error('查询失败')
      loading.value = false
    }
  })
}
function ChangeDataType(row: any) {
  const params = {
    landingId: row.landingId,
    landingStatus: row.landingStatus,
  }
  patchLandingpageType(params).then((res: any) => {
    if (res.succeeded) {
      ElMessage.success('修改成功')
      GetDataList(PageNum, PageSize)
    }
    else {
      ElMessage.error('修改失败', res.message)
      GetDataList(PageNum, PageSize)
    }
  })
}
// 页码
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return toNumber(localStorage.getItem('currentPage')) || 1
})

onMounted(() => {
  if (localStorage.getItem('currentPage') !== null) {
    GetDataList(toNumber(localStorage.getItem('currentPage')), 20)
    localStorage.removeItem('currentPage')
  }
  else {
    GetDataList()
  }
})
</script>

<template>
  <PageMain>
    <SearchBar :show-toggle="false">
      <div class="header">
        <el-form label-positio="right" label-width="auto">
          <div class="from">
            <el-form-item label="标题">
              <el-input v-model="inquireFrom.LandingName" placeholder="请输入标题" clearable @keyup.enter="Inquire" />
            </el-form-item>
          </div>
        </el-form>
        <div class="text-right">
          <el-button @click="Inquire">
            查询
          </el-button>
        </div>
      </div>
    </SearchBar>
    <ElDivider border-style="dashed" />
    <div class="mb-5 text-right">
      <el-button type="success" @click="addData">
        添加
      </el-button>
    </div>
    <el-table v-loading="loading" :data="dataList" style="width: 100%;" stripe border>
      <el-table-column align="center" prop="landingId" label="ID" width="100" />
      <el-table-column header-align="center" prop="landingName" label="标题" width="400" />
      <el-table-column align="center" prop="landingType" label="活动类型" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.landingType === 1" size="large" type="primary">
            领券
          </el-tag>
          <el-tag v-else-if="scope.row.landingType === 2" size="large" type="success">
            会员
          </el-tag>
          <el-tag v-else-if="scope.row.landingType === 3" size="large" type="info">
            直接购买
          </el-tag>
          <el-tag v-else size="large" type="danger">
            未定义
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="landingLine" label="优先" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.landingLine === 1" size="large" type="primary">
            购买优先
          </el-tag>
          <el-tag v-if="scope.row.landingLine === 2" size="large" type="success">
            领券、会员优先
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="customerServicePic" label="客服图标" width="140">
        <template #default="scope">
          <el-image v-if="scope.row.customerServiceSwitch" style="width: 100px; height: 100px;" :src="imgDomain + scope.row.customerServicePic" />
          <el-tag v-else size="large">
            不显示
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="customerServiceLink" label="客服跳转链接" width="220" />
      <el-table-column align="center" prop="buyNocouponPic" label="未领券购买按钮图片" width="380">
        <template #default="scope">
          <el-image style="width: auto; height: 70px;" :src="imgDomain + scope.row.buyNocouponPic" />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="buyCouponPic" label="券后购买按钮图片" width="380">
        <template #default="scope">
          <el-image style="width: auto; height: 70px;" :src="imgDomain + scope.row.buyCouponPic" />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="buyNovipPic" label="非会员购买图片" width="380">
        <template #default="scope">
          <el-image style="width: auto; height: 70px;" :src="imgDomain + scope.row.buyNovipPic" />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="buyVipPic" label="会员购买图片" width="380">
        <template #default="scope">
          <el-image style="width: auto; height: 70px;" :src="imgDomain + scope.row.buyVipPic" />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="buyVipPic" label="背景图" width="500">
        <template #default="scope">
          <el-scrollbar>
            <div class="scrollbar-flex-content">
              <el-image v-for="(item, index) in scope.row.landingBg" :key="index" class="scrollbar-flex-img" :src="imgDomain + item" />
            </div>
          </el-scrollbar>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="payBeforePic" label="支付前弹层" width="140">
        <template #default="scope">
          <el-image v-if="scope.row.payBeforeSwitch" style="width: 100px; height: 120px;" :src="imgDomain + scope.row.payBeforePic" />
          <el-tag v-else size="large">
            不显示
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="payAfterPic" label="支付后弹层" width="140">
        <template #default="scope">
          <el-image v-if="scope.row.payAfterSwitch" style="width: 100px; height: 120px;" :src="imgDomain + scope.row.payAfterPic" />
          <el-tag v-else size="large">
            不显示
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="scrollingSwitch" fixed="right" label="弹幕开关" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.scrollingSwitch === 0" size="large" type="primary">
            关闭
          </el-tag>
          <el-tag v-else-if="scope.row.scrollingSwitch === 1" size="large" type="success">
            开启
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="landingStatus" fixed="right" label="状态" width="100">
        <template #default="scope">
          <el-switch
            v-model="scope.row.landingStatus"
            @click="ChangeDataType(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="address" label="操作" fixed="right" min-width="200">
        <template #default="scope">
          <el-button type="primary" style="padding: 0 10px;" @click="update(scope.row.landingId)">
            编辑
          </el-button>
          <el-button type="info" style="padding: 0 10px;" @click="copy(scope.row.landingId)">
            复制地址
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="height: 10px;" />
    <el-pagination
      :page-sizes="[5, 10, 20, 50, 100]"
      layout="total, sizes, ->, prev, pager, next, jumper"
      :total="total"
      :default-current-page="nowPageNum"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </PageMain>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: 90px;
    height: 120px;
    margin-right: 10px;
  }
}
</style>
